package com.yellow.aichatservice.controller;

import com.yellow.aichatservice.service.RagService;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.io.IOException;

@RestController
@RequestMapping("/ai")
@CrossOrigin(origins = "*", maxAge = 3600)
@AllArgsConstructor
public class CloudRagController {
    private final ChatClient chatClient;

    private final RagService cloudRagService;



    @GetMapping("/bailian/knowledge/importDocument")
    public void importDocument() throws IOException {
        cloudRagService.importDocuments();
    }

    @GetMapping("/bailian/knowledge/generate")
    public Flux<String> generate(@RequestParam(value = "message",
            defaultValue = "你好，请问你的知识库文档主要是关于什么内容的?") String message) {

        return cloudRagService.retrieve(message).map(x -> x.getResult().getOutput().getText());
    }

    @GetMapping("/chat")
    public String chat(@RequestParam(value = "message",
            defaultValue = "你好，请问有什么可以帮助您的?") String message) {
        return chatClient.prompt(message).call().content();
    }

    @GetMapping("/conversation")
    public String conversation(
            @RequestParam(value = "message") String message,
            @RequestParam(value = "sessionId") String sessionId) {
        return chatClient.prompt()
                .user(message)
                .advisors(memoryAdvisor -> memoryAdvisor
                        .param(ChatMemory.CONVERSATION_ID,sessionId))
        .call().content();

    }

}